<?php
/**************************************************************************
 * Naanal PHP Framework, Simple, Efficient and Developer Friendly
 * Copyright (C) <2010>  <Tamil Amuthan. R>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ************************************************************************/

class ClsSettings extends ClsBase
{
	function __construct()
	{
		parent::__construct("settings");
	}  
	function insert_score($arrScore)
	{
		$pdo=ClsNaanalPDO::getNamedInstance(getDynamicDatabase());
		foreach($arrScore as $value=>$score)
		{
			$sql="select * from neighborhood where value='{$value}'";
			$pdo->setQuery($sql);
			$arrAssoc=$pdo->getAllAssoc();
			if(isset($arrAssoc[0]))
			{
				$sql="update neighborhood set score='{$score}' where value='{$value}'";
				$pdo->setQuery($sql);
			}
			else 
			{
				$sql="insert into neighborhood (`value`,`score`) values('{$value}','{$score}')";
				$pdo->setQuery($sql);
			}
		}
		//$sql="update adjustmentattribute set islistable=0";
		//$pdo->setQuery($sql);
	}
	function insert_field($arrField)
	{
		$sql="update adjustmentattribute set islistable=0";
		$pdo=ClsNaanalPDO::getNamedInstance(getDynamicDatabase());
		$pdo->setQuery($sql);
		if($arrField)
		foreach($arrField as $fieldID)
		{
			$sql="update adjustmentattribute set islistable=1 where id='{$fieldID}'";
			$pdo->setQuery($sql);
		}
	}
	function insert()
	{//trace($_REQUEST);
		$arrCriteria=array("criteria"=>$_REQUEST["criteria"],"criteriavalue"=>$_REQUEST["criteriavalue"]);
		$criteria=json_encode($arrCriteria);
		$sql="update usersetting set `data`='{$criteria}' where `name`='Comps Search Criteria'";
		$mypdo=ClsNaanalPDO::getNamedInstance();
		$mypdo->setQuery($sql);
		foreach($_REQUEST as $field=>$value)
		{
			if($field=="PHPSESSID" || $field=="issubmit" || $field==$this->urlActionParam || $field==$this->urlModuleParam) continue;
			if($field=="field")
			{
				$this->insert_field($value);
			}
			else if($field=="score")
			{
				$this->insert_score($value);
			}
			else 
			{
				if($field=="default_adjustment_grid")
				{
					$sql="update usersetting set `data`='{$value}' where `name`='Adjustment Grid'";
//					//trace($sql);	
				}
				else if($field=="unit")
				{
					$sql="update usersetting set `data`='{$value}' where `name`='Unit of Distance'";	
				}
				else if($field=="unit_of_currency")
				{
					$sql="update usersetting set `data`='{$value}' where `name`='Unit of Currency'";	
				}
				else if($field=="distance_sorting")
				{
					$sql="update usersetting set `data`='{$value}' where `name`='Distance Sorting'";	
				}
				else if($field=="sale_date_order")
				{
					$sql="update usersetting set `data`='{$value}' where `name`='Sale Date Order'";	
				}
				else if($field=="adjusted_value_order")
				{
					$sql="update usersetting set `data`='{$value}' where `name`='Adjusted Value Order'";	
				}
				else if($field=="zoom_level")
				{
					$sql="update usersetting set `data`='{$value}' where `name`='Zoom Level'";	
				}
				else if($field=="scale_level")
				{
					$sql="update usersetting set `data`='{$value}' where `name`='Scale Level'";	
				}
				else if($field=="comps_search_criteria")
				{
					$sql="update usersetting set `data`='{$value}' where `name`='Comps Search Criteria'";	
				}
				else if($field=="unit_of_measure")
				{
					$sql="update usersetting set `data`='{$value}' where `name`='Unit of Measure'";	
				}
				else 
				{
					$sql="update usersetting set `data`='{$value}' where `name`='{$field}'";
				}
				$mypdo->setQuery($sql);
			}
		}
		return $this->create();
	}
}
?>